<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class AuthGroup_m extends MY_Model
{
    const TBL = 'auth_groups';

    function __construct()
    {
        parent::__construct();
    }

    public function get_all_groups($status = null, $admin = null)
    {
        $this->db->select('*');
        if (!empty($status)) {
            $this->db->where('status', $status);
        }
        if (!empty($admin)) {
            $this->db->where_in('id', ['2', '7']);
        }
        $this->db->from('auth_groups');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    public function get_group_by_id($id)
    {
        $query = $this->db
            ->select('*')
            ->get_where('auth_groups', ['id' => $id]);
        return $query->row_array();
    }

    public function sava_group_rule($uid, $data)
    {

        $this->db->where('id', $uid);
        $this->db->update('auth_groups', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    public function add_group_rule($data)
    {

        $this->db->insert('auth_groups', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    public function is_user_group_able($group_id)
    {
        $query = $this->db
            ->select('status')
            ->from('auth_groups')
            ->where('id', $group_id)
            ->get();
        $ulock = $query->row_array();
        if ($ulock['status'] == 1) {
            return true;
        } else {
            return false;
        }
    }

}
